﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExaminationTT.View
{
    public partial class AlgorithmSetupForm : Form
    {
        private class WD
        {
            public DayOfWeek WeekDay { get; set; }
            public override string ToString()
            {
                return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)WeekDay];
            }
            
        }

        private class Hour
        {
            private DateTime _time;
            public Hour(DateTime time)
            {
                _time = time;
            }
            public string Description
            {
                get { return _time.ToString("HH:mm"); }
            }
            public DateTime HourTime
            {
                get { return _time; }
            }
        }

        public AlgorithmSetupForm()
        {
            this.SuspendLayout();
            InitializeComponent();
            _dgv_hours.AutoGenerateColumns = false;
            _hours = new BindingList<Hour>();
            _weekDays = new HashSet<DayOfWeek>();
            _dgv_hours.DataSource = _hours;
            Array vals = Enum.GetValues(typeof(DayOfWeek));
            foreach (DayOfWeek item in vals)
            {
                _clb_weekDays.Items.Add(
                    new WD
                    {
                        WeekDay = item
                    },false);
            }
            
            this.ResumeLayout();

        }
        private BindingList<Hour> _hours;
        private HashSet<DayOfWeek> _weekDays;
        private List<DateTime> _hoursRet;
        public IEnumerable<DayOfWeek> WeekDays
        {
            get { return _weekDays; }
        }

        public IEnumerable<DateTime> Hours
        {
            get 
            {
                if (_hoursRet == null)
                {
                    _hoursRet = new List<DateTime>();
                    foreach (var item in _hours)
                    {
                        _hoursRet.Add(item.HourTime);
                    }
                }
                return _hoursRet;
            }
        }



        

        private void _btn_addHour_Click(object sender, EventArgs e)
        {
            _hours.Add(new Hour(_dtp_weekDays_hours.Value));
        }

        private void _btn_removeHour_Click(object sender, EventArgs e)
        {
            if (_dgv_hours.SelectedRows.Count == 0) return;
            _hours.Remove((Hour)_dgv_hours.CurrentRow.DataBoundItem);
               
        }

        private void _btn_OK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Dispose();
        }

        private void _btn_Cancel_Click(object sender, EventArgs e)
        {
            DialogResult res =
                MessageBox.Show("Clique em Yes para cancelar, clique em No para correr o algoritmo sem dias e horas definidas, clique em Cancel para cancelar",
                this.Text, MessageBoxButtons.YesNoCancel);
            if (res == DialogResult.Cancel)
            {
                return;
            }
            this.DialogResult = res;
            this.Dispose();
        }

        private void _clb_weekDays_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            DayOfWeek selected =  ((WD)(_clb_weekDays.Items[e.Index])).WeekDay;
            if (e.NewValue == CheckState.Checked)
            {
                _weekDays.Add(selected);
            }
            else
            {
                _weekDays.Remove(selected);
            }

        }
    }
}
